#include "Label.h"

namespace PickinOpenSticks {
    namespace Ui {

        void Label::Draw(sf::RenderTarget *target) {
            if (!visible) return;

            //draw the background
            if (mainVisible) target->Draw(sprite);

            //then draw the text
            target->Draw(text);
        }

        void Label::SetPos(int x, int y) {
            pos.x = x;
            pos.y = y;
            sprite.SetPosition((float) x, (float) y);
            switch (alignment) {
                case Left: text.SetPosition((float) x + 5, (float) y + 5);
                    break;
                case Right: text.SetPosition((float) x + pos.w - 5 - text.GetRect().GetWidth(), (float) y + 5);
                    break;
                case Top: text.SetPosition((float) x + 5, (float) y + 5);
                    break;
                case Bottom:text.SetPosition((float) x + 5, (float) y + pos.h - 5);
                    break;
                case CenterHorizontal: text.SetPosition((float) x + pos.w / 2 - text.GetRect().GetWidth() / 2, (float) y + 5);
                    break;
                case CenterVertical: text.SetPosition((float) x + 5, (float) y + pos.h / 2 - text.GetRect().GetHeight() / 2);
                    break;
                case Center: text.SetPosition((float) x + pos.w / 2 - text.GetRect().GetWidth() / 2, (float) y + pos.h / 2 - text.GetRect().GetHeight() / 2);
                    break;
                case LeftBottom: text.SetPosition((float) x + 5, (float) y + pos.h - 5 - text.GetRect().GetWidth());
                    break;
                case LeftTop: text.SetPosition((float) x + 5, (float) y + 5);
                case RightBottom: text.SetPosition((float) x + pos.w - 5 - text.GetRect().GetWidth(), y + pos.w - 5 - text.GetRect().GetHeight());
                    break;
                case RightTop: text.SetPosition((float) x + 5, (float) y + 5);
                    break;
                default: break;
            }
        }

        void Label::Update(int diff) {
            if (!visible) return;
            Widget::Update(diff);
            if (text.GetRect().GetWidth() > pos.w)
                pos.w = (int) text.GetRect().GetWidth();
            if (text.GetRect().GetHeight() > pos.h)
                pos.h = (int) text.GetRect().GetHeight();
        }
    }
}
